package friend

import (
	"context"
	"errors"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	v1 "qiyu-contact/api/friend/v1"
	"qiyu-contact/internal/dao"
	"qiyu-contact/internal/model/entity"
	"qiyu-contact/internal/service"
)

type sFriend struct{}

func (s *sFriend) DelFriend(ctx context.Context, req *v1.DelFriendReq) (res *v1.DelFriendRes, err error) {
	identity := service.Auth().GetIdentity(ctx)
	one, err := dao.WebChatFriend.Ctx(ctx).One("user_id=? and friend_id=?", identity, req.UserId)
	if err != nil {
		return nil, err
	}
	if one == nil {
		return nil, errors.New("当前用户不是你的好友，没有权限操作")
	}
	var friend entity.WebChatFriend
	err = gconv.Struct(one, &friend)
	if err != nil {
		return nil, err
	}

	affected, err := dao.WebChatFriend.Ctx(ctx).UpdateAndGetAffected(g.Map{
		"status_cd": 0,
	}, "user_id=? and friend_id=?", friend.UserId, friend.FriendId)
	if err != nil {
		return nil, err
	}
	if affected == 0 {
		return nil, errors.New("删除失败")
	}
	return
}

// FindFriend 寻找用户
func (s *sFriend) FindFriend(ctx context.Context, req *v1.FindFriendReq) (res *v1.FindFriendRes, err error) {
	var userList []*entity.WebChatUser
	var count int
	baseDB := dao.WebChatUser.Ctx(ctx)
	if req.Account != "" {
		baseDB = baseDB.WhereOrLike("account", fmt.Sprintf("%%%s%%", req.Account))
	}
	if req.Nickname != "" {
		baseDB = baseDB.WhereOrLike("nickname", fmt.Sprintf("%%%s%%", req.Nickname))
	}
	err = baseDB.Page(req.PageNum, req.PageSize).ScanAndCount(&userList, &count, false)
	if err != nil {
		return nil, err
	}
	res = &v1.FindFriendRes{
		List:     userList,
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
		Total:    count,
	}
	return
}

// BlockFriend 拉黑用户
func (s *sFriend) BlockFriend(ctx context.Context, req *v1.BlockFriendReq) (res *v1.BlockFriendRes, err error) {
	identity := service.Auth().GetIdentity(ctx)
	one, err := dao.WebChatFriend.Ctx(ctx).One("user_id=? and friend_id=?", identity, req.UserId)
	if one == nil {
		return nil, errors.New("当前用户不是你的好友，没有权限操作")
	}
	affected, err := dao.WebChatFriend.Ctx(ctx).UpdateAndGetAffected(g.Map{
		"status_cd": 0,
	}, "user_id=? and friend_id=?", identity, req.UserId)
	if err != nil {
		return nil, err
	}
	if affected == 0 {
		return nil, errors.New("操作失败")
	}
	return
}
func (s *sFriend) AddFriend(ctx context.Context, req *v1.AddFriendReq) (res *v1.AddFriendRes, err error) {
	identity := service.Auth().GetIdentity(ctx)
	var user = &entity.WebChatUser{}
	// 寻找用户ID
	err = dao.WebChatUser.Ctx(ctx).Scan(&user, g.Map{
		"account": req.FriendAccount,
	})
	if err != nil {
		return nil, err
	}
	one, err := dao.WebChatFriend.Ctx(ctx).One(g.Map{
		"user_id":   identity,
		"friend_id": user.Id,
	})
	if one != nil {
		return nil, errors.New("对方已经是您的好友了")
	}
	if err != nil {
		return nil, err
	}
	// 插入数据
	insert, err := dao.WebChatFriend.Ctx(ctx).Insert(g.Map{
		"user_id":      identity,
		"friend_id":    user.Id,
		"user_remarks": req.Remark,
		"status_cd":    1,
	})
	if err != nil {
		return nil, err
	}
	_, err = insert.RowsAffected()
	if err != nil {
		return nil, err
	}
	return
}
func init() {
	service.RegisterFriend(&sFriend{})
}
